import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.fragment.app.Fragment
import cn.edu.swu.design.R


//package com.oy.topline.fragment
//
//import android.content.Intent
//import android.os.*
//import android.view.*
//import android.widget.LinearLayout
//import android.widget.RelativeLayout
//import android.widget.TextView
//import androidx.fragment.app.Fragment
//import androidx.recyclerview.widget.LinearLayoutManager
//import androidx.viewpager.widget.ViewPager
//import androidx.viewpager.widget.ViewPager.OnPageChangeListener


class HomeFragment : Fragment() {
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        val webView: WebView = requireActivity().findViewById(cn.edu.swu.design.R.id.webView)
    //        val webView:WebView = activity?.findViewById(R.id.webView) ?:

//        webView.settings.javaScriptEnabled=true
//        webView.webViewClient = WebViewClient()
//        webView.loadUrl("https://www.baidu.com")
//    }
//    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
//                              savedInstanceState: Bundle?): View? {
//        return inflater.inflate(cn.edu.swu.design.R.layout.fragment_home, container, false)
//    }
//    private var webView: WebView ? = null

//    private var mPullToRefreshView: PullToRefreshView? = null
//    private var recycleView: WrapRecyclerView? = null
//    private var adPager //广告
//            : ViewPager? = null
//    private var vpi //小圆点
//            : ViewPagerIndicator? = null
//    private var tvAdName //广告名称
//            : TextView? = null
//    private var adBannerLay //广告条容器
//            : View? = null
//    private var ada //适配器
//            : AdBannerAdapter? = null
//    private var mHandler //事件捕获
//            : MHandler? = null
//    private var ll_python: LinearLayout? = null
//    private var okHttpClient: OkHttpClient? = null
//    private var adapter: HomeListAdapter? = null
//    private var rl_title_bar: RelativeLayout? = null
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
//        val view = inflater.inflate(R.layout.fragment_home, container, false)
//        val webView:WebView = view.findViewById(R.id.webView)
//        webView.settings.javaScriptEnabled=true
//        webView.webViewClient = WebViewClient()
//        webView.loadUrl("https://www.baidu.com")

    //        okHttpClient = OkHttpClient()
//        mHandler = MHandler()
//        aDData
//        newsData
        return initView(inflater, container)
    }
//
    @SuppressLint("SetJavaScriptEnabled")
    private fun initView(inflater: LayoutInflater, container: ViewGroup?): View {
        val view = inflater.inflate(R.layout.fragment_home, container, false)
        val webView:WebView = view.findViewById(R.id.webView)
        webView.settings.javaScriptEnabled=true
        webView.webViewClient = WebViewClient()
        webView.loadUrl("https://www.baidu.com")
//        rl_title_bar = view.findViewById<View>(R.id.title_bar) as RelativeLayout
//        rl_title_bar!!.visibility = View.GONE
//        recycleView = view.findViewById<View>(R.id.recycler_view) as WrapRecyclerView
//        recycleView!!.layoutManager = LinearLayoutManager(context)
//        val headView = inflater.inflate(R.layout.head_view, container, false)
//        recycleView!!.addHeaderView(headView)
//        adapter = HomeListAdapter(activity)
//        recycleView!!.setAdapter(adapter)
//        mPullToRefreshView = view.findViewById<View>(
//            R.id.pull_to_refresh
//        ) as PullToRefreshView
//        mPullToRefreshView!!.setOnRefreshListener {
//            mPullToRefreshView!!.postDelayed(object : Runnable {
//                override fun run() {
//                    mPullToRefreshView!!.setRefreshing(false)
//                    aDData
//                    newsData
//                }
//            }, REFRESH_DELAY.toLong())
//        }
//        mHandler = MHandler()
//        adBannerLay = headView.findViewById(R.id.adbanner_layout)
//        adPager = headView.findViewById<View>(R.id.slidingAdvertBanner) as ViewPager
//        vpi = headView.findViewById<View>(R.id.advert_indicator) as ViewPagerIndicator
//        tvAdName = headView.findViewById<View>(R.id.tv_advert_title) as TextView
//        ll_python = headView.findViewById<View>(R.id.ll_python) as LinearLayout
//        adPager!!.isLongClickable = false
//        ada = AdBannerAdapter(activity!!.supportFragmentManager, mHandler)
//        adPager!!.adapter = ada
//        adPager!!.setOnTouchListener(ada)
//        adPager!!.setOnPageChangeListener(object : OnPageChangeListener {
//            override fun onPageSelected(index: Int) {
//                if (ada.getSize() > 0) {
//                    if (ada!!.getTitle(index % ada.getSize()) != null) {
//                        tvAdName!!.text = ada!!.getTitle(index % ada.getSize())
//                    }
//                    vpi!!.setCurrentPostion(index % ada.getSize())
//                }
//            }
//
//            override fun onPageScrolled(arg0: Int, arg1: Float, arg2: Int) {}
//            override fun onPageScrollStateChanged(arg0: Int) {}
//        })
//        resetSize()
//        setListener()
//        AdAutoSlidThread().start()
        return view
    }
//
//    private fun setListener() {
//        ll_python!!.setOnClickListener {
//            val intent = Intent(activity, PythonActivity::class.java)
//            startActivity(intent)
//        }
//    }
//
    /**
     * 事件捕获
     */
//    internal inner class MHandler : Handler() {
//        override fun dispatchMessage(msg: Message) {
//            super.dispatchMessage(msg)
//            when (msg.what) {
//                MSG_AD_SLID -> if (ada!!.count > 0) {
//                    adPager!!.currentItem = adPager!!.currentItem + 1
//                }
//                MSG_AD_OK -> if (msg.obj != null) {
//                    val adResult = msg.obj as String
//                    val adl: List<NewsBean> = JsonParse.Companion.getInstance().getAdList(adResult)
//                    if (adl != null) {
//                        if (adl.size > 0) {
//                            ada!!.setData(adl)
//                            tvAdName.setText(adl[0].newsName)
//                            vpi!!.setCount(adl.size)
//                            vpi!!.setCurrentPostion(0)
//                        }
//                    }
//                }
//                MSG_NEWS_OK -> if (msg.obj != null) {
//                    val newsResult = msg.obj as String
//                    val nbl: List<NewsBean> =
//                        JsonParse.Companion.getInstance().getNewsList(newsResult)
//                    if (nbl != null) {
//                        if (nbl.size > 0) {
//                            adapter!!.setData(nbl)
//                        }
//                    }
//                }
//            }
//        }
//    }
//
//    internal inner class AdAutoSlidThread : Thread() {
//        override fun run() {
//            super.run()
//            while (true) {
//                try {
//                    sleep(5000)
//                } catch (e: InterruptedException) {
//                    e.printStackTrace()
//                }
//                if (mHandler != null) mHandler!!.sendEmptyMessage(MSG_AD_SLID)
//            }
//        }
//    }
//
//    //开启异步线程访问网络
//    private val newsData: Unit
//        private get() {
//            val request = Request.Builder().url(
//                Constant.WEB_SITE +
//                        Constant.REQUEST_NEWS_URL
//            ).build()
//            val call = okHttpClient!!.newCall(request)
//            //开启异步线程访问网络
//            call.enqueue(object : Callback {
//                @Throws(IOException::class)
//                override fun onResponse(response: Response) {
//                    val res = response.body().string()
//                    val msg = Message()
//                    msg.what = MSG_NEWS_OK
//                    msg.obj = res
//                    mHandler!!.sendMessage(msg)
//                }
//
//                override fun onFailure(arg0: Request, arg1: IOException) {}
//            })
//        }
//
//    //开启异步线程访问网络
//    private val aDData: Unit
//        private get() {
//            val request = Request.Builder().url(
//                Constant.WEB_SITE +
//                        Constant.REQUEST_AD_URL
//            ).build()
//            val call = okHttpClient!!.newCall(request)
//            //开启异步线程访问网络
//            call.enqueue(object : Callback {
//                @Throws(IOException::class)
//                override fun onResponse(response: Response) {
//                    val res = response.body().string()
//                    val msg = Message()
//                    msg.what = MSG_AD_OK
//                    msg.obj = res
//                    mHandler!!.sendMessage(msg)
//                }
//
//                override fun onFailure(arg0: Request, arg1: IOException) {}
//            })
//        }
//
//    /**
//     * 计算控件大小
//     */
//    private fun resetSize() {
//        val sw = UtilsHelper.getScreenWidth(activity)
//        val adLheight = sw / 2 //广告条高度
//        val adlp = adBannerLay!!.layoutParams
//        adlp.width = sw
//        adlp.height = adLheight
//        adBannerLay!!.layoutParams = adlp
//    }
//
//    companion object {
//        const val REFRESH_DELAY = 1000
//        const val MSG_AD_SLID = 1 //广告自动滑动
//        const val MSG_AD_OK = 2 //获取广告数据
//        const val MSG_NEWS_OK = 3 //获取新闻数据
//    }
}